/*
 * (C) Copyright IBM Corporation 2017. All rights reserved.
 *
 */

/* 
 * Authors: 
 * 
 * Daniel G. Waddington (daniel.waddington@ibm.com)
 *
 */

#ifndef __COMANCHE_SEMAPHORE_H__
#define __COMANCHE_SEMAPHORE_H__

#include <mutex>
#include <condition_variable>

class Semaphore {
public:
  Semaphore (int count_ = 0)
    : count(count_) {}

  inline void post()
  {
    std::unique_lock<std::mutex> lock(mtx);
    count++;
    cv.notify_one();
  }

  inline void wait()
  {
    std::unique_lock<std::mutex> lock(mtx);

    while(count == 0){
      cv.wait(lock);
    }
    count--;
  }

private:
  std::mutex mtx;
  std::condition_variable cv;
  int count;
};

#endif
